<template>
  <div>
    <el-dialog :visible.sync="$parent.uploadVisible" title="上传文件" width="400px"
               @closed='closedDialog'>
      <el-upload ref="upload"
                 accept=".zip" action="/pmph/action/book/uploadFile"
                 :before-upload="beforeUpload"
                 :on-success="onSuccess" class="upload-demo" drag style='text-align:center;'>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">仅支持扩展名为.zip的文件</div>
      </el-upload>

      <span class="dialog-footer" slot="footer">
        <el-button @click="closedDialog">取 消</el-button>
        <el-button type="primary" @click="">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
export default {
  props:['uploadVisible'],
  data() {
    return {
      bookSourceFileList:[],
      percentage: 20,
      customColor: '#409eff',
      customColors: [
        {color: '#f56c6c', percentage: 20},
        {color: '#e6a23c', percentage: 40},
        {color: '#5cb87a', percentage: 60},
        {color: '#1989fa', percentage: 80},
        {color: '#6f7ad3', percentage: 100}
      ]
    }
  },
  methods: {
    async beforeUpload(file) {
      // 上传前校验文件;
      // 删除fileList当前文件;
      let index = this.bookSourceFileList.findIndex( fileItem => {
        return fileItem.name + ".zip" === file.name
      })
      if(index != -1){
        await this.$confirm("文件:"+ file.name +", 是否覆盖?", '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return true;
        })
      }
    },
    onSuccess: function(response) {
      this.$message({message: response, type: 'success', customClass: 'zZindex'});
      this.$parent.uploadVisible = false;
    },
    pro:function(){
      if (this.percentage < 30) {
        return '#909399';
      } else if (this.percentage < 70) {
        return '#e6a23c';
      } else {
        return '#67c23a';
      }
    },

    beforeRemove: function (file, fileList) {
      this.$confirm('是否删除文件？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // TODO 后台删除对应文件
        this.$message({
          type: 'success',
          message: '删除成功！'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    closedDialog() {
      this.$parent.uploadVisible = false
    }
  },
  created() {
    let that = this
    // 上传前校验文件;
    this.axios.get('/pmph/action/book/selectAllBookSourceFile').then((response) => {
      that.bookSourceFileList = response.data
    });
  }
}
</script>
<style scoped>
  .zZindex {
    z-index:3000 !important;
  }
</style>
